/**
 * Created by hcadmin on 16/3/11.
 */

define([
    "jquery",
    "IO",
    "UPLOADUI",
    "UPLOAD",
    "jqueryUI",
    "bootbox",
    //"text!/js/biz/keb/activity/definebt.html"
], function ($, IO, upui, up, jui, bootbox, definebt) {

    $('#fileupload').fileupload({
        url: '/upload',
        dataType: 'json',
        autoUpload: true,
        acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
        maxFileSize: 10000000, // 10 MB
        disableImageResize: /Android(?!.*Chrome)|Opera/
            .test(window.navigator.userAgent),
        previewMaxWidth: 150,
        previewMaxHeight: 150,
        previewCrop: true
    }).on('fileuploadadd', function (e, data) {
        document.getElementById('upimgs').src = '/images/loading.gif';

    }).on('fileuploadprocessalways', function (e, data) {
        //console.log("fileuploadprocessalways",data);
    }).on('fileuploadprogressall', function (e, data) {
        //console.log("fileuploadprogressall",data);
    }).on('fileuploaddone', function (e, data) {
        //alert('Img'+youpaiImg);
        //alert(data._response.result.url);
        //document.getElementById('upimgs').src = data._response.result.url;
        //console.log(youpaiImg);
        //  console.log("fileuploaddone",data);
        var alt = $(e.target).attr('alt');
        if (!alt)
            alt = '';
        console.log("#upimgs" + alt);
        $("#upimgs" + alt).attr("src", data._response.result.url);
    });
    $(document).on("click",".publish",function(){
        var course_name = $(".course option:selected").val();
        var course_id = $(".course option:selected").attr("data-id");
        var lecture_name = $(".lecture option:selected").val();
        //var lecture_id = $(".lecture option:selected").val();
        //选中的课时
        var checks=$(".boxWrapper input:checked");
        if(checks.length==0)
        {
            alert("请选择课时");
            return ;
        }
        var lecture_ids=[];
        $.each(checks,function(i,ele){
            lecture_ids.push($(ele).val());
        });
        lecture_ids=lecture_ids.join(",");

        var title = $("#title").val();
        var content= editor.html();
        var param = {title:title,lecture_ids:lecture_ids,lecture_name:lecture_name,course_id:course_id,content:content};


        if(title==""){
            alert("请输入案例名称");
            return;
        }
        if(course_name==""){
            alert("请选择所属课程");
            return;
        }
        if(lecture_name==""){
            alert("请选择所属课时");
            return;
        }
        if(content==""){
            alert("请输入案例内容");
            return;
        }

        if(param&&param!='undefined'){
            bootbox.confirm("确定要添加该案例",function(result){
                if(result) {
                    IO.post("/pte/act/add_case",param,function(d){
                        bootbox.alert(d.msg,function(){
                            if (d.code == 0) {
                                location.href = '/pte/case_lb';
                            }
                        })
                    });
                }
            });
        }
    })

    $(".course").on("change",function(){
        IO.post("/pte/api/getCourseLecture",{course_id:$(this).val()},function(d){
            var html="";
            for(var i=0;i< d.length;i++){
                html+='<input type="checkbox" value="'+d[i].lecture_id+'" />'+d[i].lecture_name+"&nbsp;&nbsp;&nbsp;"
            }
           // document.getElementsByClassName("lecture")[0].innerHTML=html;
            $(".boxWrapper").html(html);
        });

    })
});

